package com.example.testapp.activity.dabastate;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.testapp.activity.model.CalendarTag;
import com.example.testapp.activity.model.CalendarVO;

import java.util.ArrayList;

/**
 * Created by l on 2016/6/3.
 */
public class CalendarDao {

    private DBOpenHelper dbOpenHelper = null;

    public CalendarDao(Context context) {
         dbOpenHelper = new DBOpenHelper(context, "schedules.db");
    }

    /**
     * 保存日程信息
     * @param calendarVO
     */
    public int save(CalendarVO calendarVO) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("CalendarTypeID", calendarVO.getScheduleTypeID());
        values.put("remindID", calendarVO.getRemindID());
        values.put("CalendarContent", calendarVO.getScheduleContent());
        values.put("CalendarDate", calendarVO.getScheduleDate());
        db.beginTransaction();
        int CalendarID = -1;
        try{
            db.insert("Calendar", null , values);
            Cursor cursor = db.rawQuery("select max(CalendarID) from Calendar", null);
            if(cursor.moveToFirst()){
                 CalendarID = (int) cursor.getLong(0);
            }
            cursor.close();
            db.setTransactionSuccessful();
        }finally {
            db.endTransaction();
        }
        return CalendarID;
    }

    /**
     * 只查询出当前月的日程日期
     * @param currentYear
     * @param currentMonth
     * @return
     */
    public ArrayList<CalendarTag> getTagDate(int currentYear, int currentMonth) {
        ArrayList<CalendarTag> dateTagList = new ArrayList<CalendarTag>();
        SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
        Cursor  cursor = db.query("CalendarTagDate", new String[]{"TagID", "year", "month", "day", "CalendarID"}, "year=? and month=?", new String[]{String.valueOf(currentYear), String.valueOf(currentMonth)}, null, null, null);
        while (cursor.moveToFirst()){
            int tagID = cursor.getInt(cursor.getColumnIndex("TagID"));
            int year = cursor.getInt(cursor.getColumnIndex("year"));
            int month = cursor.getInt(cursor.getColumnIndex("month"));
            int day = cursor.getInt(cursor.getColumnIndex("day"));
            int calendarID = cursor.getInt(cursor.getColumnIndex("CalendarID"));
            CalendarTag   dateTag = new CalendarTag(tagID, year, month, day, calendarID);
            dateTagList.add(dateTag);
        }
        cursor.close();
        if(dateTagList != null && dateTagList.size() > 0){
            return dateTagList;
        }
        return null;
    }

    /**
     * 查询某一条日程信息
     * @param CalendarID
     * @return
     */
    public CalendarVO getScheduleByID(int CalendarID) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        Cursor cursor = db.query("Calendar", new String[]{"CalendarID","CalendarTypeID","remindID","CalendarContent","CalendarDate"}, "CalendarID=?", new String[]{String.valueOf(CalendarID)}, null, null, null);
        if (cursor.moveToFirst()){
            int CalID = cursor.getInt(cursor.getColumnIndex("CalendarID"));
            int CalendarTypeID = cursor.getInt(cursor.getColumnIndex("CalendarTypeID"));
            int remindID = cursor.getInt(cursor.getColumnIndex("remindID"));
            String CalendarContent = cursor.getString(cursor.getColumnIndex("CalendarContent"));
            String CalendarDate = cursor.getString(cursor.getColumnIndex("CalendarDate"));
            cursor.close();
            return new CalendarVO(CalID, CalendarTypeID, remindID, CalendarContent, CalendarDate);
        }
        cursor.close();
        return null;
    }

    /**
     * 当点击每一个gridview中item时,查询出此日期上所有的日程标记(scheduleID)
     * @param year
     * @param month
     * @param day
     * @return
     */
    public String[] getScheduleByTagDate(int year, int month, int day) {
        SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
        //根据时间查询出日程ID（scheduleID），一个日期可能对应多个日程ID
        Cursor cursor = db.query("CalendarTagDate", new String[]{"CalendarID"}, "year=? and month=? and day=?", new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(day)}, null, null, null);
        String calendarIDs[] = null;
        calendarIDs = new String[cursor.getCount()];
        int i = 0;
        while (cursor.moveToNext()){
            String calendarID = cursor.getString(cursor.getColumnIndex("CalendarID"));
            calendarIDs[i] = calendarID;
            i++;
        }
        cursor.close();
        return calendarIDs;
    }

    /**
     * 将日程标志日期保存到数据库中
     * @param dateTagList
     */
    public void saveTagDate(ArrayList<CalendarTag> dateTagList) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        CalendarTag  dateTag = new CalendarTag();
        for (int i = 0; i<dateTagList.size(); i++){
            dateTag = dateTagList.get(i);
            ContentValues values = new ContentValues();
            values.put("year", dateTag.getYear());
            values.put("month", dateTag.getMonth());
            values.put("day", dateTag.getDay());
            values.put("CalendarID", dateTag.getScheduleID());
            db.insert("CalendarTagDate", null, values);
        }
    }

    /**
     * 删除日程
     * @param calendarID
     */
    public void delete(int calendarID) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.beginTransaction();
        try {
            db.delete("Calendar","CalendarID=?",new String[]{String.valueOf(calendarID)});
            db.delete("CalendarTagDate", "CalendarID=?", new String[]{String.valueOf(calendarID)});
            db.setTransactionSuccessful();
        }finally {
            db.endTransaction();
        }
    }

    /**
     * 查询所有的日程信息
     * @return
     */
    public ArrayList<CalendarVO> getAllCalendar() {
        ArrayList<CalendarVO>  list = new ArrayList<CalendarVO>();
        SQLiteDatabase  db = dbOpenHelper.getWritableDatabase();
        Cursor cursor = db.query("Calendar", new String[]{"CalendarID", "CalendarTypeID", "remindID", "CalendarContent", "CalendarDate"}, null, null, null, null, "CalendarID desc");
        while (cursor.moveToNext()){
            int calendarID = cursor.getInt(cursor.getColumnIndex("CalendarID"));
            int scheduleTypeID = cursor.getInt(cursor.getColumnIndex("CalendarTypeID"));
            int remindID = cursor.getInt(cursor.getColumnIndex("remindID"));
            String scheduleContent = cursor.getString(cursor.getColumnIndex("CalendarContent"));
            String scheduleDate = cursor.getString(cursor.getColumnIndex("CalendarDate"));
            CalendarVO  vo = new CalendarVO(calendarID, scheduleTypeID, remindID, scheduleContent,scheduleDate);
            list.add(vo);
        }
        cursor.close();
        if(list != null && list.size() > 0){
            return list;
        }
        return null;
    }
}
